package cn.shyouchi.wuguanjia.android.ui.casualrecord;

import android.view.View;
import android.widget.CheckBox;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;

import java.util.ArrayList;
import java.util.List;

import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.common.Constant;

/**
 * Created by Eenie on 2017/3/28 at 14:36
 * Email: 472279981@qq.com
 * Des:
 */

public class ChooseAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {


    public ChooseAdapter(List<MultiItemEntity> data) {
        super(data);
        addItemType(Constant.ITEM_TYPE_GROUP, R.layout.item_sel_meet_person_layout);
        addItemType(Constant.ITEM_TYPE_LEVEL1, R.layout.item_sel_meet_person_level1_layout);
    }


    @Override
    protected void convert(final BaseViewHolder holder, MultiItemEntity entity) {


        switch (entity.getItemType()) {
            case Constant.ITEM_TYPE_GROUP:
                if (entity instanceof ChooseGroup) {
                    final ChooseGroup group = (ChooseGroup) entity;
                    final CheckBox raGroup = holder.getView(R.id.ck_person_name);
                    raGroup.setText(group.getName());


                    raGroup.setChecked(group.isSelected());

                    raGroup.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            toogleGroupState(group, raGroup.isChecked());
                        }
                    });


                    holder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            int pos = holder.getAdapterPosition();
                            if (group.isExpanded()) {
                                collapse(pos);
                            } else {
                                expand(pos);
                            }
                        }
                    });
                }
                break;


            case Constant.ITEM_TYPE_LEVEL1:
                final ChooseBean level = (ChooseBean) entity;
                final CheckBox raGroup = holder.getView(R.id.ck_person_name);
                raGroup.setText(level.getName());
                raGroup.setChecked(level.isSeleted());
                raGroup.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        level.setSeleted(raGroup.isChecked());
                        notifyDataSetChanged();
                    }
                });
                break;

        }


    }


    private void toogleGroupState(ChooseGroup group, boolean seleted) {
        group.setSelected(seleted);
        for (ChooseBean chooseBean : group.getSubItems()) {
            chooseBean.setSeleted(seleted);
        }
        notifyDataSetChanged();
    }


    public ArrayList<Integer> getSelected() {

        ArrayList<Integer> ids = new ArrayList<>();

        for (MultiItemEntity entity : getData()) {

            if (entity instanceof ChooseBean) {
                ChooseBean bean = (ChooseBean) entity;
                ids.add(bean.getUser_id());
            }
        }
        return ids;
    }


}
